<?php
final class PageUtil {
	/**
	 * 获取指定页码页面的url
	 */
	public static function getPageUrl($pageNum) {
		$cfg = Page::$cfg;
		
		//$_GET may destroy in script
		$getArr = array();
		parse_str($_SERVER['QUERY_STRING'], $getArr);
		$params = !empty($cfg['params']) ? $cfg['params'] : $getArr;
		$params = array_merge($params, array($cfg['pageKey'] => $pageNum));
		
		$baseUrl = !empty($cfg['baseUrl']) ? $cfg['baseUrl'] : self::getCurrentBaseUrl();
		
		return $baseUrl . '?' . http_build_query($params);
	}
	
	/**
	 * 获取当前页码
	 */
	public static function getCurrentPageNum() {
		$cfg = Page::$cfg;
		$firstRow = max(0, intval($cfg['firstRow']));
		return (int)floor($firstRow / $cfg['listRows'] + 1);
	}
	
	/**
	 * 获取总页数
	 */
	public static function getTotalPage() {
		$cfg = Page::$cfg;
		
		return (int)ceil($cfg['totalRows'] / $cfg['listRows']);
	}
	
	/**
	 * get current page url
	 */
	public static function getCurrentUrl() {
		$scheme = (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != "on") ? 'http' : 'https';
		$port = (($scheme === 'http' && $_SERVER['SERVER_PORT'] != '80') || ($scheme === 'https' && $_SERVER['SERVER_PORT'] != '443')) ? ":{$_SERVER['SERVER_PORT']}" : "";
		$currentUrl = $scheme . '://' . $_SERVER['SERVER_NAME'] . $port . $_SERVER['REQUEST_URI'];
		
		return $currentUrl;
	}
	
	/**
	 * get current page base url
	 */
	public static function getCurrentBaseUrl() {
		$scheme = (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != "on") ? 'http' : 'https';
		$port = (($scheme === 'http' && $_SERVER['SERVER_PORT'] != '80') || ($scheme === 'https' && $_SERVER['SERVER_PORT'] != '443')) ? ":{$_SERVER['SERVER_PORT']}" : "";
		$currentBaseUrl = $scheme . '://' . $_SERVER['SERVER_NAME'] . $port . $_SERVER['SCRIPT_NAME'];
		
		return $currentBaseUrl;
	}
}
?>